package com.lzb.newloan;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.adapter.OverdueListAdapter;
import com.lzb.newloan.pojo.BorrowPersonPojo;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.Formatdou;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.utils.TimeUtils;
import com.lzb.newloan.xlistview.XListView;
import com.lzb.newloan.xlistview.XListView.IXListViewListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MyOverDueListActivity extends BaseActivity implements
		OnClickListener, IXListViewListener {

	private Button one, two, three, four,all;
	private XListView list_overdue;
	private ArrayList<BorrowPersonPojo> arrayList;
	private OverdueListAdapter adapter;
	private int curPage=1;
	private boolean isloadMore;
	private String userId;
	private String minOverdueday="",maxOverdueday="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_overdue_list);
		initView();
		initData();
	}

	private void initView() {
		SharedPreferences sp = getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");
		one = (Button) findViewById(R.id.one);
		one.setOnClickListener(this);
		two = (Button) findViewById(R.id.two);
		two.setOnClickListener(this);
		three = (Button) findViewById(R.id.three);
		three.setOnClickListener(this);
		four = (Button) findViewById(R.id.four);
		four.setOnClickListener(this);
		all = (Button) findViewById(R.id.all);
		all.setOnClickListener(this);
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(this);
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("逾期列表");
		overdue = (TextView) findViewById(R.id.overdue);
		
		list_overdue = (XListView)findViewById(R.id.list_overdue);
		arrayList = new ArrayList<BorrowPersonPojo>();
//		 for (int i = 0; i < 5; i++) {
//				 BorrowPersonPojo pojo = new BorrowPersonPojo();
//				 arrayList.add(pojo);
//				 }
		adapter = new OverdueListAdapter(this, arrayList,3);
		list_overdue.setAdapter(adapter);
		list_overdue.setXListViewListener(this);
		list_overdue.setPullLoadEnable(false);
		list_overdue.setPullRefreshEnable(true);
	}
	
	

	private void initData() {
		HttpUtils.doPostAsyn(Config.OVERDUELIST_CORD + "&userid="
				+ userId+"&minOverdueday="+minOverdueday+"&maxOverdueday="+maxOverdueday+"&curPage="+curPage,
				"",mHandler, Config.CODE_OVERDUELIST);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.all:
			all.setBackgroundColor(getResources().getColor(R.color.green));
			all.setTextColor(getResources().getColor(R.color.white));
			one.setBackgroundColor(getResources().getColor(R.color.white));
			one.setTextColor(getResources().getColor(R.color.textcolor));
			two.setBackgroundColor(getResources().getColor(R.color.white));
			two.setTextColor(getResources().getColor(R.color.textcolor));
			three.setBackgroundColor(getResources().getColor(R.color.white));
			three.setTextColor(getResources().getColor(R.color.textcolor));
			four.setBackgroundColor(getResources().getColor(R.color.white));
			four.setTextColor(getResources().getColor(R.color.textcolor));
			minOverdueday="";
			maxOverdueday="";
			arrayList.clear();
			initData();
			
			break;
		case R.id.one:
			all.setBackgroundColor(getResources().getColor(R.color.white));
			all.setTextColor(getResources().getColor(R.color.textcolor));
			one.setBackgroundColor(getResources().getColor(R.color.green));
			one.setTextColor(getResources().getColor(R.color.white));
			two.setBackgroundColor(getResources().getColor(R.color.white));
			two.setTextColor(getResources().getColor(R.color.textcolor));
			three.setBackgroundColor(getResources().getColor(R.color.white));
			three.setTextColor(getResources().getColor(R.color.textcolor));
			four.setBackgroundColor(getResources().getColor(R.color.white));
			four.setTextColor(getResources().getColor(R.color.textcolor));
			minOverdueday="1";
			maxOverdueday="7";
			arrayList.clear();
			initData();
			break;
		case R.id.two:
			all.setBackgroundColor(getResources().getColor(R.color.white));
			all.setTextColor(getResources().getColor(R.color.textcolor));
			one.setBackgroundColor(getResources().getColor(R.color.white));
			one.setTextColor(getResources().getColor(R.color.textcolor));
			two.setBackgroundColor(getResources().getColor(R.color.green));
			two.setTextColor(getResources().getColor(R.color.white));
			three.setBackgroundColor(getResources().getColor(R.color.white));
			three.setTextColor(getResources().getColor(R.color.textcolor));
			four.setBackgroundColor(getResources().getColor(R.color.white));
			four.setTextColor(getResources().getColor(R.color.textcolor));
			minOverdueday="8";
			maxOverdueday="15";
			arrayList.clear();
			initData();
			break;
		case R.id.three:
			all.setBackgroundColor(getResources().getColor(R.color.white));
			all.setTextColor(getResources().getColor(R.color.textcolor));
			one.setBackgroundColor(getResources().getColor(R.color.white));
			one.setTextColor(getResources().getColor(R.color.textcolor));
			two.setBackgroundColor(getResources().getColor(R.color.white));
			two.setTextColor(getResources().getColor(R.color.textcolor));
			three.setBackgroundColor(getResources().getColor(R.color.green));
			three.setTextColor(getResources().getColor(R.color.white));
			four.setBackgroundColor(getResources().getColor(R.color.white));
			four.setTextColor(getResources().getColor(R.color.textcolor));
			minOverdueday="16";
			maxOverdueday="30";
			arrayList.clear();
			initData();
			break;
		case R.id.four:
			all.setBackgroundColor(getResources().getColor(R.color.white));
			all.setTextColor(getResources().getColor(R.color.textcolor));
			one.setBackgroundColor(getResources().getColor(R.color.white));
			one.setTextColor(getResources().getColor(R.color.textcolor));
			two.setBackgroundColor(getResources().getColor(R.color.white));
			two.setTextColor(getResources().getColor(R.color.textcolor));
			three.setBackgroundColor(getResources().getColor(R.color.white));
			three.setTextColor(getResources().getColor(R.color.textcolor));
			four.setBackgroundColor(getResources().getColor(R.color.green));
			four.setTextColor(getResources().getColor(R.color.white));
			minOverdueday="31";
			maxOverdueday="";
			arrayList.clear();
			initData();
			break;
		case R.id.backpress:
			finish();
			break;
		default:
			break;
			
		}
	}
	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case Config.CODE_OVERDUELIST:
					String reString = msg.obj.toString();
					try {
						JSONObject response = new JSONObject(reString);
						JSONObject pageObject = response.getJSONObject("page");
						int totalPages = pageObject.getInt("totalPages");
						if (curPage < totalPages) {
							list_overdue.setPullLoadEnable(true);
						} else {
							list_overdue.setPullLoadEnable(false);
						}
						ArrayList<BorrowPersonPojo> arrayList2 = new ArrayList<BorrowPersonPojo>();
						JSONArray array = pageObject.getJSONArray("data");
						if (array.length()<=0) {
							overdue.setVisibility(View.VISIBLE);
							list_overdue.setVisibility(View.GONE);
						}else {
							overdue.setVisibility(View.GONE);
							list_overdue.setVisibility(View.VISIBLE);
							for (int i = 0; i < array.length(); i++) {
								BorrowPersonPojo pojo = new BorrowPersonPojo();
								JSONObject object = (JSONObject) array.get(i);
								pojo.setUsername(object.getString("cardusername"));
								JSONObject hkyq_time = object.getJSONObject("loan_date");
								pojo.setTime(TimeUtils.parseDate2(Long.parseLong(hkyq_time.getString("time"))));
								JSONObject fkdz_time = object.getJSONObject("sys_repay_time");
								pojo.setFkTime(TimeUtils.parseDate2(Long.parseLong(fkdz_time.getString("time"))));
								double money = object.getDouble("actual_amount");
								double lx = object.getDouble("annual_rate");
//							pojo.setSpdz("http://sd-shipin.oss-cn-shenzhen.aliyuncs.com/"+object.getString("spdz"));
								pojo.setSp_money(Formatdou.formatdou(money));
								pojo.setId(object.getString("id"));
								pojo.setDay(object.getInt("over_days")+"天");
								pojo.setId(object.getString("id"));
								pojo.setLiving_photo(object.getString("living_photo"));
								pojo.setLx(Formatdou.formatdou(lx));
								arrayList2.add(pojo);
							}

							if (isloadMore) {
								arrayList.addAll(arrayList2);

							} else {
								arrayList = arrayList2;
							}

							adapter.setArrayList(arrayList);
							adapter.notifyDataSetChanged();
						}
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					break;
				case Config.CODE_URL_ERROR:
					Toast.makeText(MyOverDueListActivity.this, "url错误",
							Toast.LENGTH_SHORT).show();
					break;
				case Config.CODE_NET_ERROR:
					Toast.makeText(MyOverDueListActivity.this, "网络错误",
							Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
			}
		};
	};

	private TextView overdue;
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// list.clear();
				curPage = 1;
				isloadMore = false;
				initData();
				onLoad();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				curPage += 1;
				isloadMore = true;
				initData();
				onLoad();
			}
		}, 2000);
	}

	protected void onLoad() {
		list_overdue.stopRefresh();
		list_overdue.stopLoadMore();
		list_overdue.setRefreshTime(TimeUtils.getDate());
	}

	@Override
	public void processMessage(Message message) {

	}

}
